#!/bin/bash
#
# Script to download item and collections from steam workshop.
# Repository of this script:
# https://notabug.org/kitsune-chan/dsteam
#
# Based on the official steamcmd client:
# https://developer.valvesoftware.com/wiki/SteamCMD
#
# Sorry for the not very readable code, I'm a stupid little fox 
#


# Default variables
CACHE_DIR=$HOME/.cache/dsteam
DOWNLOAD_DIR=$HOME/downloads/dsteam
STEAMCMD_PATH=$HOME/Steam/steamcmd.sh


# Manual and options
while [[ -n $1 ]]; do
        case "$1" in
                -h | --help) 
                        echo "Script to download items and collections from Steam Workshop"
                        echo -e "\tdsteam [OPTIONS] ID"
                        echo
                        echo
                        echo "OPTIONS:"
                        echo -e "\t-h, --help"
                        echo -e "\t\tPrint help message for this script"
                        echo
                        echo -e "\t-d [PATH], --download_dir [PATH]"
                        echo -e "\t\tSpecify download directory"
                        echo -e "\t\tdefault: \$HOME/dsteam"
                        echo
                        echo -e "\t-s [PATH], --steamcmd_path [PATH]"
                        echo -e "\t\tSpecify the location of SteamCMD"
                        echo -e "\t\tdefault: \$HOME/Steam/steamcmd.sh"
                        exit 0
                        ;;
                -d | --download_dir)
                        DOWNLOAD_DIR=$2
                        ;;
                -s | --steamcmd_path)
                        STEAMCMD_PATH=$2
                        ;;
                *)
                        DOWNLOAD_ID=$1
                        ;;
        esac
        shift
done


# Check steamcmd
if [ ! -f $STEAMCMD_PATH ]; then
    echo "EXIT: SteamCMD was not found at the specified path. Please check the spelling of the path."
    exit 0
fi


# Check ID
if [[ ! -n $DOWNLOAD_ID ]]; then
        echo "EXIT: Please specify the ID."
        exit 0
fi

if [[ ! $DOWNLOAD_ID =~ ^[0-9]+$ ]]; then
        echo "EXIT: Invalid ID."
        exit 0
fi


# Making directories
mkdir -p $CACHE_DIR 
mkdir -p $DOWNLOAD_DIR


# Downloading steam page 
echo "[ $(date +%T) ] Downloading steam page... "
curl -s "https://steamcommunity.com/sharedfiles/filedetails/?id=$DOWNLOAD_ID" > $CACHE_DIR/page


# Get information from page
GAMEID=$(cat ~/.cache/dsteam/page | grep -o "\"appid\":[0-9]*" | grep -o -m 1 [0-9].*)
sed -i '1,/class="detailBox"/ d' $CACHE_DIR/page
sed -i '/class="childrenTitle"/,$d' $CACHE_DIR/page
cat $CACHE_DIR/page | grep -wv "class=\"bb_link\"" |
                      grep -o "https://steamcommunity.com/sharedfiles/filedetails/?id=[0-9]*" |
                      grep -o "[0-9]*" |
                      sort -u > $CACHE_DIR/IDs


# Check IDs
if [[ ! -s "$CACHE_DIR/IDs" ]]; then
        echo "EXIT: Download ID not found."
        rm -rf $CACHE_DIR/dsteam
        exit 0
fi
readarray -t IDs < $CACHE_DIR/IDs


# Making script for steamcmd
echo "force_install_dir $DOWNLOAD_DIR" > $CACHE_DIR/script
echo "login anonymous" >> $CACHE_DIR/script
for ID in ${IDs[@]}; do
        echo "workshop_download_item $GAMEID $ID validate" >> $CACHE_DIR/script
done
echo "quit" >> $CACHE_DIR/script


# Downloading items
echo "[ $(date +%T) ] Starting download mods!"
$STEAMCMD_PATH +runscript $CACHE_DIR/script | while read OUTPUT
do
        echo $OUTPUT >> $CACHE_DIR/log
        if [[ $OUTPUT =~ "Success" ]] || [[ "$OUTPUT" =~ "ERROR" ]] || [[ $OUTPUT =~ "workshop_download_item" ]]; then
                echo $OUTPUT | grep -o -e "Success" -e "ERROR"
                printf "[ $(date +%T) ] "
                ID=$(echo $OUTPUT | grep -o "workshop_download_item .*" | sed "s|workshop_download_item $GAMEID ||" | sed "s| validate||")
                if [[ ! -z $ID ]]; then
                        printf "Downloading ID $ID... "
                fi
        fi
done


# End
echo "Downloading done, Master! ^^"
rm -rf $CACHE_DIR/dsteam
